package com.example.beautymakeup.utils;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;


@Component
public class TimeUtil {
    public String getTime(String dbTimestamp){
        Instant currentInstant = Instant.now();

        // 将数据库时间戳转换为Instant对象
        Instant dbInstant = Instant.ofEpochMilli(Long.parseLong(dbTimestamp));

        // 计算时间差
        Duration duration = Duration.between(dbInstant, currentInstant);

        // 定义时间差的阈值
        long secondsThreshold = 60;
        long minutesThreshold = 60 * secondsThreshold;
        long hoursThreshold = 24 * minutesThreshold;
        long daysThreshold = 30 * hoursThreshold;

        // 根据时间差返回不同的格式
        String formattedTimeDiff;
        LocalDateTime dbDateTime = LocalDateTime.ofInstant(dbInstant, ZoneId.systemDefault());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SS");
        formattedTimeDiff = dbDateTime.format(formatter);

        return formattedTimeDiff;
    }
}
